﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Api.Model;

namespace Api.Web.Tests
{
    [TestClass]
    public class Delete
    {
        Db db = Db.Current;

        [TestMethod]
        public void DeleteNull()
        {
            //直接移除一個argumentnull的錯
            db.Projects.Remove(null);
            db.SaveChanges();
        }

        [TestMethod]
        public void EntryNullObj()
        {
            //直接就是argumentnullexception
            Project project = null;
            var entry = db.Entry(project);
        }

        [TestMethod]
        public void DeleteNotNull()
        {
            //ok
            Project project = null;
            if (project != null)
                db.Projects.Remove(project);
            var num = db.SaveChanges();
            Assert.AreEqual(0, num);
        }

        #region 這裡主要為了說明，當project的Id是一個數據庫中不存在的數據時，結果會拋出一個異常DbUpdateConcurrencyException
        [TestMethod]
        public void Delete4()
        {
            var project = new Project { Id = 10 };
            var entry = db.Entry(project);
            if (entry != null)
                entry.State = System.Data.Entity.EntityState.Deleted;
            db.SaveChanges();
        }
        [TestMethod]
        public void Delete5()
        {
            var project = new Project { Id = 1 };
            var entry = db.Entry(project);
            if (entry != null)
                entry.State = System.Data.Entity.EntityState.Deleted;
            db.SaveChanges();
        }
        #endregion

    }
}
